﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Xml;
using System.Collections;
using System.Threading;
using System.Diagnostics;


//File.AppendAllText(roude,"0\n");
//File.AppendAllText(roude, "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\n");
//File.AppendAllText(roude, "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000");

namespace Battleship
{
    class readWriteFile
    {
        public string strPanel1 = "";
        public string strPanel2 = "";
        public string strPanel3 = "";
        public string strPanel4 = "";
        public static string roude = Battleship.Properties.Settings.Default.FilePath/*"game.txt"*/;

        public void availabilityFile()//создание файла
        {
            //MessageBox.Show("Фаил игры отсутствует!");
            FileStream fs = File.Create(roude);
            StreamWriter sw = new StreamWriter(fs);
            sw.WriteLine("0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000");//строка превого игрока
            sw.WriteLine("0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000");//строка второго игрока
            sw.WriteLine("1");//строка кто ходит
            sw.WriteLine("1");//строка количества играков
            sw.WriteLine("0");//строка - количество готовых игроков
            sw.Close();
        }

        public string readFileTXT(int id)//чтение данных с txt файла
        {
            string str = "";
            string line = "";

            StreamReader file = null;
            do
            {
                try
                {
                    file = new StreamReader(roude);
                }
                catch
                {
                    Thread.Sleep(75);
                }
            }
            while (file == null);

            ArrayList arrL = new ArrayList();
            while ((line = file.ReadLine()) != null)
            {
                arrL.Add(line);               
            }
            file.Close();
            if (id == 1)
            {
                str = arrL[0].ToString();
            }
            else
            {
                str = arrL[1].ToString();
            }
            return str;
        }

        public void writeFileTXT(string str, int id/*номер строки*/)//запись в txt файл
        {
            string line = "";
            StreamReader file = null;
            do
            {
                try
                {
                    file = new StreamReader(roude);
                }
                catch
                {
                    Thread.Sleep(75);
                }
            }
            while (file == null);
            ArrayList arrL = new ArrayList();
            while ((line = file.ReadLine()) != null)
            {
                arrL.Add(line);
            }
            file.Close();
            FileStream fs = null;
            do
            {
                try
                {
                    fs = new FileStream(roude, FileMode.Create);
                }
                catch
                {
                    Thread.Sleep(75);
                }
            }
            while (fs == null);
            StreamWriter sw = null;
            do
            {
                try
                {
                    sw = new StreamWriter(fs);
                }
                catch
                {
                    Thread.Sleep(75);
                }
            }
            while (sw == null);
            if (id == 1)
            {
                sw.WriteLine(str);
                sw.WriteLine(arrL[1].ToString());
                sw.WriteLine(arrL[2].ToString());
                sw.WriteLine(arrL[3].ToString());
                sw.WriteLine(arrL[4].ToString());
            }
            else if (id == 2)
            {
                sw.WriteLine(arrL[0].ToString());
                sw.WriteLine(str);
                sw.WriteLine(arrL[2].ToString());
                sw.WriteLine(arrL[3].ToString());
                sw.WriteLine(arrL[4].ToString());
            }
            else if (id == 3)
            {
                sw.WriteLine(arrL[0].ToString());
                sw.WriteLine(arrL[1].ToString());
                sw.WriteLine(str);
                sw.WriteLine(arrL[3].ToString());
                sw.WriteLine(arrL[4].ToString());
            }
            else if (id == 4)
            {
                sw.WriteLine(arrL[0].ToString());
                sw.WriteLine(arrL[1].ToString());
                sw.WriteLine(arrL[2].ToString());
                sw.WriteLine(str);
                sw.WriteLine(arrL[4].ToString());
            }
            else if (id == 5)
            {
                sw.WriteLine(arrL[0].ToString());
                sw.WriteLine(arrL[1].ToString());
                sw.WriteLine(arrL[2].ToString());
                sw.WriteLine(arrL[3].ToString());
                sw.WriteLine(str);
            }
            sw.Close();

        }

        public int whoMove()//кто ходит
        {
            string line = "";
            string ch = "";
            int n = 0;
            StreamReader file = null;
            do
            {
                try
                {
                    file = new StreamReader(roude);
                }
                catch
                {
                    Thread.Sleep(75);
                }
            }
            while (file == null);
            ArrayList arrL = new ArrayList();
            while ((line = file.ReadLine()) != null)
            {
                arrL.Add(line);
            }
            file.Close();
            ch = arrL[2].ToString();
            n = Convert.ToInt32(ch);
            return n;
        }

        public int howManyPlayers()//сколько игроков
        {
            string line = "";
            string ch = "";
            int n = 0;
            StreamReader file = null;
            do
            {
                try
                {
                    file = new StreamReader(roude);
                }
                catch
                {
                    Thread.Sleep(75);
                }
            }
            while (file == null);
            ArrayList arrL = new ArrayList();
            while ((line = file.ReadLine()) != null)
            {
                arrL.Add(line);
            }
            file.Close();
            ch = arrL[3].ToString();
            n = Convert.ToInt32(ch);
            return n;
        }

        public int howManyReadyPlayers()//сколько готовых игроков
        {
            string line = "";
            string ch = "";
            int n = 0;
            StreamReader file = null;
            do
            {
                try
                {
                    file = new StreamReader(roude);
                }
                catch
                {
                    Thread.Sleep(75);
                }
            }
            while (file == null);
            ArrayList arrL = new ArrayList();
            while ((line = file.ReadLine()) != null)
            {
                arrL.Add(line);
            }
            file.Close();
            ch = arrL[4].ToString();
            n = Convert.ToInt32(ch);
            return n;
        }

    }
}

